//Death to the forms lib!! --Jon88
//The login form now uses the almost-as-friendly-as-forms-lib Page datum instead
//Page datum related info may or may not be in comments somewhere else

proc/BrowseTestColourImage()
	var/icon/I = icon('white.dmi', "blue")
	I.SwapColor(rgb(0,0,255),rgb(usr.Red,usr.Green,usr.Blue))
	usr << browse_rsc(I, "blue.png")

Page/LoginForm
	window = "Loginform"
	browse_settings = "size=478x500;display=1;clear=0;border=1;can_close=0;can_resize=0;can_minimize=0;titlebar=0"
	GetPage(href, href_list)
		if(StartsWith(href, "LoginForm"))
			BrowseTestColourImage()
			content = {"<HTML><head>
<STYLE type="text/css">
.bar { font-family: Courier }
HR.bar { color: blue }
Table.bar { border-color: black; color: blue; background-color: blue; border-style : solid; border-width: thin}
body {color:black;background:#FFFFFF}
A:link { color:red; text-decoration:none }
A:visited { color:red; text-decoration:none }
A:active { color:red; text-decoration:none }
A:hover { color: yellow; text-decoration: underline }
</style>
</head>
<body scroll=no onLoad="javascript:DoUpdate()"><Center>
<SCRIPT language="Javascript">
var Available, Strength, Intelligence, Constitution, Dexterity, PixPerLvl, MinLvl, NoPoints, BarWidth, submitLink, num, button, okbutton, temp;
Available = [usr.AvailablePoints];
Strength = [usr.Str];
Intelligence = [usr.Int];
Constitution = [usr.Con];
Dexterity = [usr.Dex];
PixPerLvl = 1;
MinLvl = 20;
MaxLvl = 72;
NoPoints = "You do not have enough points to do that";
BarWidth = 20;

function ResetVals() {
Available = [usr.AvailablePoints];
Strength = [usr.Str];
Intelligence = [usr.Int];
Constitution = [usr.Con];
Dexterity = [usr.Dex];
DoUpdate()
}


function UpdateBars() {
if(Available > 9) {
temp = "<TABLE class='bar' height='" + Available * PixPerLvl + "'><TR><TD width='" + BarWidth + "'></TD></TR></TABLE>";
Swap("Avail", temp);
}
if(Available < 4) {
Swap("Avail", "");
}
temp = "<TABLE class='bar' height='" + Strength * PixPerLvl + "'><TR><TD width='" + BarWidth + "'></TD></TR></TABLE>";
Swap("Str", temp);
temp = "<TABLE class='bar' height='" + Intelligence * PixPerLvl + "'><TR><TD width='" + BarWidth + "'></TD></TR></TABLE>";
Swap("Int", temp);
temp = "<TABLE class='bar' height='" + Constitution * PixPerLvl + "'><TR><TD width='" + BarWidth + "'></TD></TR></TABLE>";
Swap("Con", temp);
temp = "<TABLE class='bar' height='" + Dexterity * PixPerLvl + "'><TR><TD width='" + BarWidth + "'></TD></TR></TABLE>";
Swap("Dex", temp);
}

function UpdateVals() {
document.login.AvailVal.value = Available;
document.login.StrVal.value = Strength;
document.login.IntVal.value = Intelligence;
document.login.ConVal.value = Constitution;
document.login.DexVal.value = Dexterity;
}

function DoUpdate() {
UpdateBars();
UpdateVals();
}

function AddStr(num) {
if (!(Available - num + 1 > 0)) {
num = Available
}
if (Strength + num < MaxLvl + 1) {
Available -= num
Strength += num
}
else {
Available -= MaxLvl - Strength
Strength = MaxLvl
}
DoUpdate()
}

function SubStr(num) {
if (Strength - num > MinLvl - 1) {
Strength -= num
Available += num
DoUpdate()
}
else alert(NoPoints)
}

function AddInt(num) {
if (!(Available - num + 1 > 0)) {
num = Available
}
if (Intelligence + num < MaxLvl + 1) {
Available -= num
Intelligence += num
}
else {
Available -= MaxLvl - Intelligence
Intelligence = MaxLvl
}
DoUpdate()
}

function SubInt(num) {
if (Intelligence - num > MinLvl - 1) {
Intelligence -= num
Available += num
DoUpdate()
}
else alert(NoPoints)
}

function AddCon(num) {
if (!(Available - num + 1 > 0)) {
num = Available
}
if (Constitution + num < MaxLvl + 1) {
Available -= num
Constitution += num
}
else {
Available -= MaxLvl - Constitution
Constitution = MaxLvl
}
DoUpdate()
}

function SubCon(num) {
if (Constitution - num > MinLvl - 1) {
Constitution -= num
Available += num
DoUpdate()
}
else alert(NoPoints)
}

function AddDex(num) {
if (!(Available - num + 1 > 0)) {
num = Available
}
if (Dexterity + num < MaxLvl + 1) {
Available -= num
Dexterity += num
}
else {
Available -= MaxLvl - Dexterity
Dexterity = MaxLvl
}
DoUpdate()
}

function SubDex(num) {
if (Dexterity - num > MinLvl - 1) {
Dexterity -= num
Available += num
DoUpdate()
}
else alert(NoPoints)
}

function Swap(ID, content) {
//if IE 4+
                  if (document.all) {
                  temp = ID + ".innerHTML=content;";
                  eval(temp);
                  }
                  //else if NS 6 (supports new DOM)
                  else if (document.getElementById){
                  rng = document.createRange();
                  el = document.getElementById(ID);
                  rng.setStartBefore(el);
                  htmlFrag = rng.createContextualFragment(content);
                  while (el.hasChildNodes())
                  el.removeChild(el.lastChild);
                  el.appendChild(htmlFrag);
                  }
                  }

function numbersOnly() {
if (event.keyCode<48||event.keyCode>57)
	return false
}

function setButton(buttonnum) {
document.login.button.value = buttonnum
}

function nothing() {
return false
}

function ReallySubmit() {
	if(Available > 0) {
		var x = confirm("Undistributed points available, exit anyway?")
		return x
	}
	return 1
}
</SCRIPT>

<FORM name="login" method="GET">
<INPUT type="hidden" name="action" value="LoginForm">
<table width="95%" height="353" border="1" bordercolor="#000000" frame="box" rules="none" cellpadding="10">
  <tr>
    <td>
      <table width="100%" frame="box" rules="groups">
        <tr>
          <td height="28" colspan="5">Character name: <INPUT TYPE="text" name="name" value="[usr.name]" length=20></td>
        </tr>
<tr valign="bottom">
          <td width="21%" height="122"><DIV align="right"></DIV><DIV ID="Avail"></DIV></td>
          <td width="19%"><table><tr><td valign="bottom"><DIV ID="Str"></DIV></td><td valign="bottom"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddStr(10)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubStr(10)" CLASS="bar"><HR WIDTH="75%"><INPUT TYPE="BUTTON" VALUE="+" onClick='javascript:AddStr(1)' CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubStr(1)" CLASS="bar"></td></tr></table></td>
          <td width="20%"><table><tr><td valign="bottom"><DIV ID="Int"></DIV></td><td valign="bottom"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddInt(10)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubInt(10)" CLASS="bar"><HR WIDTH="75%"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddInt(1)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubInt(1)" CLASS="bar"></td></tr></table></td>
          <td width="21%"><table><tr><td valign="bottom"><DIV ID="Con"></DIV></td><td valign="bottom"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddCon(10)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubCon(10)" CLASS="bar"><HR WIDTH="75%"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddCon(1)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubCon(1)" CLASS="bar"></td></tr></table></td>
          <td width="19%"><table><tr><td valign="bottom"><DIV ID="Dex"></DIV></td><td valign="bottom"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddDex(10)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubDex(10)" CLASS="bar"><HR WIDTH="75%"><INPUT TYPE="BUTTON" VALUE="+" onClick="javascript:AddDex(1)" CLASS="bar"><BR><INPUT TYPE="BUTTON" VALUE="-" onClick="javascript:SubDex(1)" CLASS="bar"></td></tr></table></td>
        </tr>
        <tr>
          <td height="23">Available<BR><INPUT type="text" value="300" name="AvailVal" length=3  size=3 onkeypress="return nothing()"></td>
          <td>Strength<BR><INPUT type="text" name="StrVal" length=3  size=3 onkeypress="return nothing()"></td>
          <td>Intelligence<BR><INPUT type="text" name="IntVal" length=3  size=3 onkeypress="return nothing()"></td>
          <td>Constitution<BR><INPUT type="text" name="ConVal" length=3  size=3 onkeypress="return nothing()"></td>
          <td>Dexterity<BR><INPUT type="text" name="DexVal" length=3  size=3 onkeypress="return nothing()"></td>
        </tr>
        <tr>
          <td height="58" colspan="2"><B>Gender</B> "}
			if(usr.gender == "female")
				content += {"<p>Male <INPUT type="radio" name="gender" value="Male">Female <INPUT type="radio" name="gender" value="Female" checked></p></td> "}
			else
				content += {"<p>Male <INPUT type="radio" name="gender" value="Male" checked>Female <INPUT type="radio" name="gender" value="Female"></p></td> "}
			content += {"<td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td height="23" colspan="5"><B>Colourize</B></td>
        </tr>
        <tr>
          <td height="61"> <p>Red</p>
            <p><INPUT type="text" name="Rvar" length=3 size=3 width=3 value="[usr.Red]" onkeypress="return numbersOnly()"></p></td>
          <td><p>Green</p>
            <p><INPUT type="text" name="Gvar" length=3 size=3 width=3 value="[usr.Green]" onkeypress="return numbersOnly()"></p></td>
          <td><p>Blue</p>
            <p><INPUT type="text" name="Bvar" length=3 size=3 width=3 value="[usr.Blue]" onkeypress="return numbersOnly()"></p>
            </td>
          <td colspan="2"><p>Your Colour: <img src = "blue.png" align="middle"><INPUT type="submit" value="Apply Colour" onClick="javascript:setButton(1)"></p>
            </td>
        </tr>
      </table>
    </tr>
  </td>
</table>
<p> <INPUT type="submit" value="Ok" onClick="javascript:setButton(2); return ReallySubmit()"> <INPUT type="submit" value="Quit" onClick="javascript:setButton(3)"> <INPUT type="submit" value="About" onClick="javascript:setButton(4)"></p>
<INPUT type="hidden" name="button" value="2"></FORM>
<p>&nbsp;</p>
</BODY>
</HTML> "}
			return src


/* Take the input from the player's form and do a sanity check for correctness.
If it appears that a player has attempted to "improve" their stats beyond the norm,
be mean to them. :D
*/

	ProcessPage(href, href_list)
		var/Rvar = text2num(href_list["Rvar"])
		var/Bvar = text2num(href_list["Bvar"])
		var/Gvar = text2num(href_list["Gvar"])
		var/Name = href_list["name"]
		usr.Str = text2num(href_list["StrVal"])
		usr.Con = text2num(href_list["ConVal"])
		usr.Int = text2num(href_list["IntVal"])
		usr.Dex = text2num(href_list["DexVal"])
		usr.AvailablePoints = text2num(href_list["AvailVal"])
		// Make sure they don't have their stats set too high
		usr.Str = min(usr.Str, 72)
		usr.Con = min(usr.Con, 72)
		usr.Int = min(usr.Int, 72)
		usr.Dex = min(usr.Dex, 72)
		// Make sure they don't have their stats set too low
		usr.Str = max(usr.Str, 20)
		usr.Con = max(usr.Con, 20)
		usr.Int = max(usr.Int, 20)
		usr.Dex = max(usr.Dex, 20)
		usr.AvailablePoints = max(usr.AvailablePoints, 0)
		// If they have too many points, first clear out AvailablePoints
		if(usr.Str + usr.Con + usr.Int + usr.Dex + usr.AvailablePoints > 280)
			usr.AvailablePoints = 0
		// While they have too many points, subtract points
		while(usr.Str + usr.Con + usr.Int + usr.Dex + usr.AvailablePoints > 280)
			usr.Str--
			usr.Con--
			usr.Int--
			usr.Dex--
		switch(text2num(href_list["button"]))
			if(1) // Apply colours to form
				if(!Rvar)
					Rvar = 1
				if(!Bvar)
					Bvar = 1
				if(!Gvar)
					Gvar = 1
				usr.Red = Rvar
				usr.Blue = Bvar
				usr.Green = Gvar
				usr.name = Name
				var/Page/P = FindPage("loginform")
				P.UnDisplayPage(usr)
				P.DisplayPage(usr)
			if(2)
				var/brightness
				UnDisplayPage(usr)
				switch(href_list["gender"])
					if("female")
						usr.icon_state = "f"
					if("male")
						usr.icon_state = "m"
				if(Name)
					sd_htmlremove(Name)
					usr.name = Name
				else
					usr.name = usr.ckey
				usr.Red = text2num(href_list["Rvar"])
				usr.Blue = text2num(href_list["Bvar"])
				usr.Green = text2num(href_list["Gvar"])
				if(href_list["gender"] == "female")
					usr.Gender = href_list["gender"]
				else
					usr.Gender = "male"
				usr.ClickedNew = 0
				usr.TurnController.RemoveMob(usr) // Get rid of the turncontroller they're on from title
				usr.NewTurnController() // Give them a new one
				usr.Teleport(locate(36,14,1)) // Teleport them to starting location
				usr.Locked = 0
				FindSpecialIcon(usr)
				var/icon/I = new(usr.icon)
				I.SwapColor(rgb(0,0,255),rgb(usr.Red,usr.Green,usr.Blue))
				usr.icon = I
				players_list += usr
				var/rounded = (usr.Red*30 + usr.Green*59 + usr.Blue*11)
				brightness = round(rounded/100,1) // <----- split into above pieces
				if(brightness > 153)
					usr.Red = round(usr.Red*0.6)
					usr.Blue = round(usr.Blue*0.6)
					usr.Green = round(usr.Green*0.6)
				usr.text = {"<font color=[rgb(usr.Red,usr.Green,usr.Blue)]>@</font>"}
				world <<"<font color=[rgb(usr.Red,usr.Green,usr.Blue)]>[usr]</font></font> has entered the realm"
				usr.client.lazy_eye = 4
				for(var/i = 1, i <= 10, i++)
					usr.hudspells[i] = ""
				usr.canNewLoad = 1
				usr.savable = 1
				usr.Pause = 1
				var/obj/object/Container/Bag/Small_bag/B = new() // ADDING BAG!
				B.Move(usr)
				B.Force_equip(usr, "pack")
				var/obj/object/Container/Purse/P = new() // ADDING BAG!
				P.Move(usr)
				P.Force_equip(usr, "purse")
			if(3)
				usr.ClickedNew = 0
				UnDisplayPage(usr)
			if(4)
				var/Page/P = FindPage("about")
				P.DisplayPage(usr)


Page/About
	GetPage(href, href_list)
		window = "about"
		if(StartsWith(href, "about"))
			content = {"
<style>
body {color:black;background:#E9E9E9}
A:link { color:red; text-decoration:none }
A:visited { color:red; text-decoration:none }
A:active { color:red; text-decoration:none }
A:hover { color: yellow; text-decoration: underline }
</style>
 <B>Created by Maz, then and now it is created by the FourWinds team .</B><BR> Castle of the Winds is originally a rogue-like created by Rick Saada for Saadasoft and Epic-Megagames.
 It was split into two parts, the first part being '<i>A Question of Vengeance</I>' where you are set out to avenge your Fathers death. The second being '<i>Lifthransir's Bane</i>' where you quest to reclaim your throne.
 <BR><font color = blue> The FourWinds are <a href = "http://www.byond.com/people/maz">Maz</a>, <a href = "http://www.byond.com/people/splattergnome">splattergnome</a> and <a href = "http://www.byond.com/people/jon88">Jon88</a>.</font>
 <BR><font color = blue> We are by no means assosiated with Saadasoft and Epic-Megagames. We do have written permission for the game though.
"}
			return src

/*Shadowdarke: var/sd_color/C = new()
Shadowdarke: then set the rgb values to the submitted values
Shadowdarke: C.red = red_value, and so on
Maz: ok, but i do this for every mob?
Shadowdarke: then check the brightness()*/k